# coding=utf-8
# 第三章代码
import tensorflow as tf


def compute_graph():
    # 图中变量不共享
    g1 = tf.Graph()
    with g1.as_default():
        ini = tf.zeros_initializer()
        tf.get_variable("v", initializer=ini(shape=[1]))

    g2 = tf.Graph()
    with g2.as_default():
        ini = tf.ones_initializer()
        tf.get_variable("v", initializer=ini(shape=[1]))

    with tf.Session(graph=g1) as sess:
        tf.initialize_all_variables().run()
        with tf.variable_scope("", reuse=True):
            print(sess.run(tf.get_variable("v")))

    with tf.Session(graph=g2) as sess:
        tf.initialize_all_variables().run()
        with tf.variable_scope("", reuse=True):
            print(sess.run(tf.get_variable("v")))


def tensor_introduce():
    a = tf.constant([1.0, 2.0], name='a')
    b = tf.constant([2.0, 3.0], name='b')
    result = a + b
    print(result)
    sess = tf.Session()
    with sess.as_default():
        print(result.eval())


def tf_variable():
    # 1. 张量的类型固定后就不能改变的
    # w1 = tf.Variable(tf.random_normal([2, 3], stddev=1))
    # w2 = tf.Variable(tf.random_normal([2, 3], dtype=tf.float64, stddev=1))
    # # 将 w1 赋值给w2
    # w1.assign(w2)
    # iniop = tf.initialize_all_variables()
    # with tf.Session() as sess:
    #     sess.run(iniop)
    #     print(sess.run(w1))
    #     print(sess.run(w2))

    # 2. 维度可以改变，但是validate_shape 需要设置为 False
    w1 = tf.Variable(tf.random_normal([2, 3], stddev=1))
    w2 = tf.Variable(tf.random_normal([2, 2], stddev=1))
    # 将 w1 赋值给w2
    ass = tf.assign(w1, w2, validate_shape=False)
    init_op = tf.initialize_all_variables()
    with tf.Session() as sess:
        sess.run(init_op)
        print(w1)
        print(w2)
        sess.run(ass)
        print(w1)
        print(w2)


if __name__ == '__main__':
    tf_variable()
